/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;


import ecom.shell.ShellRemote;

public class Product extends AbstractPage {

    private String products,  name;
    private double min,  max;
    private int critere;

    public String showByCateg() throws Exception {
        //RECUPERATION DU SHELL
        ShellRemote shell = (ShellRemote) getSession().get("shell");

        String[] idCateg = (String[]) this.getParameters().get("id");

        if (idCateg != null && !"".equalsIgnoreCase(idCateg[0])) {
            products = shell.executeCommand("product -categ " + idCateg[0]);
        }
        this.setPage("product.jsp");
        return SUCCESS;
    }

    public String showByStore() throws Exception {
        //RECUPERATION DU SHELL
        ShellRemote shell = (ShellRemote) getSession().get("shell");

        String[] idStore = (String[]) this.getParameters().get("id");

        if (idStore != null && !"".equalsIgnoreCase(idStore[0])) {
            products = shell.executeCommand("product -store " + idStore[0]);
        }
        this.setPage("product.jsp");
        return SUCCESS;
    }

    public String showByName() throws Exception {
        //RECUPERATION DU SHELL
        ShellRemote shell = (ShellRemote) getSession().get("shell");

        if (critere == 1) {
            products = shell.executeCommand("product -name " + name);
        } else if (critere == 2) {
            products = shell.executeCommand("product -like " + name);
        } else {
            String[] paramName = (String[]) this.getParameters().get("name");

            if (paramName != null && !"".equalsIgnoreCase(paramName[0])) {
                products = shell.executeCommand("product -name " + paramName[0]);
            }
        }
        this.setPage("product.jsp");
        return SUCCESS;
    }

    public String showByPrice() throws Exception {
        //RECUPERATION DU SHELL
        ShellRemote shell = (ShellRemote) getSession().get("shell");

        products = shell.executeCommand("product -price " + min + " " + max);

        this.setPage("product.jsp");
        return SUCCESS;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    @Override
    public void validate() {
        this.setPage("welcome.jsp");
        String[] requestAction = (String[]) getParameters().get("action");

        if (requestAction != null && "search_name".equalsIgnoreCase(requestAction[0])) {
            if (name.length() == 0) {
                this.addFieldError("name", "Préciser le nom");
            }
        }
    }

    public String getProducts() {
        return products;
    }

    /* SEARCH OPTION */
    public void setMax(double max) {
        this.max = max;
    }

    public void setMin(double min) {
        this.min = min;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCritere(int critere) {
        this.critere = critere;
    }
}
